package com.hyd.tweet.twitter.command;

import com.hyd.tweet.twitter.TwitterUserUtils;
import com.hyd.tweet.twitter.TwitterUtils;
import com.hyd.tweet.user.User;
import com.hyd.tweet.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import twitter4j.Twitter;
import twitter4j.TwitterException;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * (description)
 *
 * @author yiding.he
 */
public abstract class Command {

    static final Logger log = LoggerFactory.getLogger(Command.class);

    public static Command getCommand(String cmd) {
        String title = cmd.split("\\s+")[0].trim();

        if (title.startsWith("/") || title.startsWith("-")) {
            title = title.substring(1);

            if (title.equals("help")) {
                return new HelpCommand();

            } else if (title.equals("g")) {
                return new GetCommand();

            } else if (title.equals("u")) {
                return new UpdateCommand();

            } else if (title.equals("start")) {
                return new StartCommand();

            } else if (title.equals("stop")) {
                return new StopCommand();

            } else if (title.equals("sleep")) {
                return new SetSleepHoursCommand();

            } else if (title.equals("atme")) {
                return new AtMeCommand();

            } else if (title.equals("rt")) {
                return new RetweetCommand();

            } else if (title.equals("rp")) {
                return new ReplyCommand();

            } else if (title.equals("ls")) {
                return new ListCommand();

            } else if (title.equals("fo")) {
                return new FollowCommand();

            } else if (title.equals("unfo")) {
                return new UnfollowCommand();

            } else if (title.equals("info")) {
                return new InfoCommand();

            } else if (title.equals("int")) {
                return new SetIntervalCommand();

            } else if (title.equals("debug")) {
                return new DebugCommand();

            } else if (title.equals("del")) {
                return new DeleteTweetCommand();

            } else if (title.equals("fav")) {
                return new AddFavoriteCommand();

            } else if (title.equals("unfav")) {
                return new DeleteFavoriteCommand();

            } else if (title.equals("m")) {
                return new DirectMessageCommand();

            } else if (title.equals("spam")) {
                return new ReportSpamCommand();

            } else if (title.equals("block")) {
                return new BlockCommand();

            } else if (title.equals("unblock")) {
                return new UnblockCommand();

            } else if (title.equals("s")) {
                return new SearchCommand();

            } else if (title.matches("^\\d{1,2}$")) {
                return new ShowTweetCommand();

            } else {
                return null;
            }

        } else {
            return new SendStatusCommand();
        }
    }

    protected String commandText;           // 命令原文

    protected List<String> arguments;       // 解析出来的参数

    protected Twitter twitter;              // Twitter 访问对象

    protected User user;                    // 用户信息

    protected twitter4j.User twitterUser;   // 用户的 twitter 账号

    /**
     * 处理命令
     *
     * @param cmd  用户输入的完整命令
     * @param user 用户信息
     *
     * @return 执行结果
     *
     * @throws TwitterException 如果执行失败
     */
    public List<String> process(String cmd, User user) throws TwitterException {
        List<String> arguments = extractArguments(cmd);

        if (arguments.get(0).startsWith("/") || arguments.get(0).startsWith("-")) {
            arguments.remove(0);  // 去掉命令，只留下参数
        }

        this.commandText = cmd;
        this.arguments = arguments;
        this.user = user;
        this.twitter = TwitterUtils.getTwitter(user);
        this.twitterUser = TwitterUserUtils.getCurrentTwitterUser(user);

        return processCommand();
    }

    private static ArrayList<String> extractArguments(String cmd) {
        ArrayList<String> list = new ArrayList<String>(Arrays.asList(cmd.split("\\s+")));
        StringUtils.removeEmptyElements(list);
        return list;
    }

    protected abstract List<String> processCommand() throws TwitterException;

    /**
     * 当任务处理失败时，是否将错误信息显示给用户，子类可覆写。
     *
     * @return 如果需要显示错误信息给用户，则返回 true
     */
    public boolean showErrorsToUser() {
        return true;
    }
}
